import { Global, Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { ConfigurationService } from './services/configuration.service';
import { TypeOrmModule } from '@nestjs/typeorm';
import { User } from './entities/user.entity';
import { UserService } from './services/user.services';
import { UtilityService } from './services/utility.services';
import { IsUsernameUniqueConstraint } from './validators/user-validator';
@Global()
@Module({
  imports: [
    ConfigModule.forRoot({ isGlobal: true }),
    TypeOrmModule.forRootAsync({
      imports: [ConfigModule],
      inject: [ConfigurationService],
      useFactory: (ConfigurationService: ConfigurationService) => ({
        type: 'mysql', //连接数据库类型
        ...ConfigurationService.mysqlConfig,
        autoLoadEntities: true, //自动加载所有实体
        synchronize: true, //保持代码和数据库一致
        logging: true, //打印内部真正执行的SQL语句
      }),
    }),
    //注册实体
    TypeOrmModule.forFeature([User]),
  ],
  exports: [
    IsUsernameUniqueConstraint,
    ConfigurationService,
    UserService,
    UtilityService,
  ],
  providers: [
    IsUsernameUniqueConstraint,
    ConfigurationService,
    UserService,
    UtilityService,
  ],
})
export class SharedModule {}
